package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentCalibration;
import com.mxpio.erp.common.equipment.entity.EquipmentCalibrationStatusRecord;
import com.mxpio.erp.common.equipment.service.EquipmentCalibrationService;
import com.mxpio.erp.common.equipment.service.EquipmentCalibrationStatusRecordService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "EquipmentCalibrationStatusRecordController}", description = "设备检定-检定状态记录")
@RestController
@RequestMapping("/erp/equipment/calibration/statusRecord")
public class EquipmentCalibrationStatusRecordController {

    @Autowired
    private EquipmentCalibrationStatusRecordService statusRecordService;

    @Autowired
    private EquipmentCalibrationService equipmentCalibrationService;


    @GetMapping("list")
    @Operation(summary = "状态记录", description = "获取状态记录（无分页)", method = "GET")
    public Result<List<EquipmentCalibrationStatusRecord>> list(@Parameter(description = "查询参数")Criteria criteria){
        List<EquipmentCalibrationStatusRecord> list = statusRecordService.list(EquipmentCalibrationStatusRecord.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "状态记录", description = "获取状态记录（分页)", method = "GET")
    public Result<Page<EquipmentCalibrationStatusRecord>> page(@Parameter(description = "查询参数")Criteria criteria,
                                                               @Parameter(description = "每页条数")Integer pageSize,
                                                               @Parameter(description = "页号")Integer pageNo){
        PageRequest page = PageRequest.of(pageNo - 1, pageSize);
        Page<EquipmentCalibrationStatusRecord> listPage = statusRecordService.listPage(EquipmentCalibrationStatusRecord.class, page, criteria);
        return  Result.OK(listPage);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取检定状态列表", description = "根据ID获取检定状态列表", method = "GET")
    public Result<EquipmentCalibrationStatusRecord> getById(@PathVariable(name = "id", required = true)String id){
        EquipmentCalibrationStatusRecord byId = statusRecordService.getById(EquipmentCalibrationStatusRecord.class, id);
        return Result.OK(byId);
    }
    @RequestMapping("add")
    @Operation(summary = "保存检定状态", description = "保存检定状态", method = "POST")
    public Result<EquipmentCalibrationStatusRecord> add(@RequestBody EquipmentCalibrationStatusRecord statusRecord){
        EquipmentCalibrationStatusRecord save = statusRecordService.save(statusRecord);
        return Result.OK(save);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑检定状态", description = "编辑检定状态", method = "PUT")
    public Result<EquipmentCalibrationStatusRecord> edit(@RequestBody EquipmentCalibrationStatusRecord statusRecord){
        EquipmentCalibrationStatusRecord update = statusRecordService.update(statusRecord);
        return Result.OK(update);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除检定状态", description = "删除设备状态,多个id用英文逗号分隔", method = "DELETE")
    public Result<EquipmentCalibrationStatusRecord> remove(@PathVariable(name = "ids", required = true)String ids){
        String[] id = ids.split(",");
        for (String key : id) {
            statusRecordService.delete(EquipmentCalibrationStatusRecord.class, key);
        }
        return Result.OK();
    }

    @PutMapping("listExecute/{calCode}")
    public Result<EquipmentCalibration> listExecute(@PathVariable(name = "calCode", required = true)String calCode){
        EquipmentCalibration calibration = equipmentCalibrationService.getById(EquipmentCalibration.class, calCode);
        equipmentCalibrationService.listExecute(calibration);
        return Result.OK();
    }

}
